package com.fy56.platform.logistics.service.impl;

import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.mapper.AdvertiseMapper;
import com.fy56.platform.logistics.model.Advertise;
import com.fy56.platform.logistics.service.web.IAdvertiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by Ning on 2018/5/9.
 */
@Service
public class AdvertiseServiceImpl implements IAdvertiseService {

    @Autowired
    private AdvertiseMapper advertiseMapper;

    /*
    * @parameter
    * limit 查询条数  默认5条
    * type  倒序字段     0:createTime   1:updateTime
    * */
    @Override
    public List<Advertise> findHomePageAdvertiseList(Map<String,Object> par) {
        return advertiseMapper.findHomePageAdvertiseList(par);
    }

    @Override
    public Advertise selectByPrimaryKey(Long id) {
        return advertiseMapper.selectByPrimaryKey(id);
    }

    @Override
    public Advertise get(Advertise entity) {
        return null;
    }

    @Override
    public ResultModel insert(Advertise entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insertSelective(Advertise entity) {
        ResultModel result = null;
        int r = advertiseMapper.insertSelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    public ResultModel update(Advertise entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(Advertise entity) {
        ResultModel result = null;
        int r = advertiseMapper.updateByPrimaryKeySelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    public ResultModel delete(Long id) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel batchDelete(List<Advertise> list) {
        LinkedList<String[]> failedIds = new LinkedList<String[]>();
        if(null != list && !list.isEmpty()){
            int r = 0;
            for(Advertise entity : list){
                r = advertiseMapper.updateDeleteFlagByPrimaryKey(entity);
                if (r == 0) {
                    failedIds.add(new String[]{entity.getId().toString(), "批量禁用失败!"});
                }
            }
        }
        if(failedIds.size() > 0){
            return new ResultModel(0, "", failedIds);
        }else{
            return ResultModel.ok();
        }
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Advertise entity) {
        ResultModel result = null;
        int r = advertiseMapper.updateDeleteFlagByPrimaryKey(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    public List<Advertise> findList(Advertise advertise) {
        return null;
    }
}
